import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/ui/patrol/patrol_record_detail_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/patrol/patrol_record_view_model.dart';

class PatrolRecordListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _PatrolRecordListState();
}

class _PatrolRecordListState
    extends BaseMvvmListState<PatrolRecordListViewModel, PatrolRecordListPage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Color(0xfff1f1f1),
      appBar: AppBar(
        title: Text('巡防记录'),
      ),
      body: Column(
        children: [
          SizedBox(
            height: 16,
          ),
          Row(
            children: [
              Row(
                children: [
                  CommonInput(
                    hintText: "输入任务名称搜索",
                    isDense: true,
                    border: InputBorder.none,
                    padding: EdgeInsets.zero,
                    maxLines: 1,
                    textInputAction: TextInputAction.search,
                    onChanged: (str) {
                      viewModel.nameLike = str;
                    },
                    onSubmitted: (str) {
                      viewModel.requestRefresh();
                    },
                  ).setWeight(1),
                  Icon(
                    Icons.search,
                    size: 20,
                  ).onTap(() {
                    viewModel.requestRefresh();
                  })
                ],
              )
                  .addToContainer(
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: 20.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
              )
                  .setWeight(1),
              SizedBox(
                width: 16,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  CommonText(viewModel.startDateStr, hintText: '选择时间筛选'),
                  SizedBox(
                    width: 10,
                  ),
                  Icon(
                    viewModel.startDateStr.isEmpty
                        ? Icons.arrow_drop_down_outlined
                        : Icons.clear,
                    size: 20,
                  ).onTap(() {
                    if (viewModel.startDateStr.isNotEmpty) {
                      setState(() {
                        viewModel.startDateStr = "";
                        viewModel.startDateTime = null;
                        viewModel.requestRefresh();
                      });
                    }
                  })
                ],
              )
                  .onTap(() {
                viewModel.selectStartDate(click: (str) {
                  setState(() {
                    viewModel.startDateStr = str;
                  });
                  viewModel.requestRefresh();
                });
              })
                  .addToContainer(
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: 20.borderRadius),
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6))
                  .setWeight(1)
            ],
          ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16)),
          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }



  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            CommonText(item.name).setWeight(1),
            CommonText(item.resultStatusName,
                textColor: item.resultStatus == 1
                    ? Colors.blue
                    : item.resultStatus == 2
                        ? Colors.green
                        : Colors.red)
          ],
        ),
        Container(
          height: 10,
        ),
        CommonText('日期：${item.startDate?.changeTime(format: [
              yyyy,
              '-',
              mm,
              '-',
              dd
            ])}'),
        Container(
          height: 10,
        ),
        CommonText(
            '时间段：${item.startDate?.changeTime()}-${item.endDate?.changeTime()}'),
        Container(
          height: 10,
        ),
        CommonText('巡防点总数：${item.pointTotal}'),
        Container(
          height: 8,
        ),
        RichText(
            text: TextSpan(
                text: '已巡查数量：',
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.black,
                ),
                children: [
              TextSpan(
                  text: '${item.finishedNum}',
                  style: TextStyle(fontSize: 16, color: Colors.green))
            ])),
        Container(
          height: 8,
        ),
        RichText(
            text: TextSpan(
                text: '未巡查数量：',
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.black,
                ),
                children: [
              TextSpan(
                  text: '${item.unfinishedNum}',
                  style: TextStyle(fontSize: 16, color: Colors.red))
            ]))
      ],
    ).onTap(() {
      Go().push(PatrolRecordDetailPage(
        item: item
      ));
    }).addToContainer(
        width: double.infinity,
        padding: EdgeInsets.all(10),
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 10.borderRadius));
  }

  @override
  PatrolRecordListViewModel createViewModel() {
    return PatrolRecordListViewModel(context);
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(horizontal: 16, vertical: 16);

  @override
  Widget get separatorDivider => Container(
        height: 10,
      );

  @override
  bool get canPullUp => true;
}
